﻿//-----------------------------------------------------------------------
// <summary>
//  A module in Prism is a logical unit in your application.
// </summary>
// <copyright file="ResearchControl.cs" company="DSSoftLogic">
//     Copyright@ DSSoftLogic 2011, Non-Commercial. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Property.Silverlight.Control
{
    using System;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// The research control.
    /// </summary>
    public class ResearchControl : ItemsControl
    {
        /// <summary>
        /// The headers property.
        /// </summary>
        public static readonly DependencyProperty HeadersProperty =
            DependencyProperty.Register("Headers", typeof(ObservableCollection<object>), typeof(ResearchControl), null);

        /// <summary>
        /// Initializes a new instance of the <see cref="ResearchControl"/> class.
        /// </summary>
        public ResearchControl()
        {
            this.DefaultStyleKey = typeof(ResearchControl);
            this.Headers = new ObservableCollection<object>();
        }

        /// <summary>
        /// Gets the headers.
        /// </summary>
        public ObservableCollection<object> Headers
        {
            get { return (ObservableCollection<object>)GetValue(HeadersProperty); }
            private set { this.SetValue(HeadersProperty, value); }
        }

        /// <summary>
        /// The on items changed.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            base.OnItemsChanged(e);
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                object newItem = e.NewItems[0];
                DependencyObject header = GetHeader(newItem as FrameworkElement);
                this.Headers.Insert(e.NewStartingIndex, header);
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                this.Headers.RemoveAt(e.OldStartingIndex);
            }
        }

        /// <summary>
        /// The get header.
        /// </summary>
        /// <param name="view">
        /// The view.
        /// </param>
        /// <returns>
        /// The <see cref="DependencyObject"/>.
        /// </returns>
        private static DependencyObject GetHeader(FrameworkElement view)
        {
            if (view != null)
            {
                DataTemplate template = view.Resources["HeaderIcon"] as DataTemplate;
                if (template != null)
                {
                    return template.LoadContent();
                }
            }

            return null;
        }
    }
}
